import request from '@/utils/request.js'

/**
* 根据频道获取频道
* @param {Number} channelId - 频道ID
* @param {Number} timestamp - 时间戳
* @returns
*/
export const getArticlesByChannel = (channelId, timestamp) => {
  return request({
    url: '/v1_0/articles',
    method: 'get',
    params: {
      channel_id: channelId,
      timestamp
    }
  })
}
/**
 *
 * @param {*} id
 * @returns 当前频道的详情
 */
export const getArticlesDetailId = (id) => {
  return request({
    url: '/v1_0/articles/:article_id',
    article_id: id
  })
}
/**
* 获取文章详情
* @param {String} id - 文章ID
* @returns
*/
export const getArticle = (id) => {
  return request({
    url: `/v1_0/articles/${id}`
  })
}

/**
* 获取文章评论
* @param {String} articleId - 文章ID
* @param {String} offset - 上一页数据最后一个ID，做为下一页请求的偏移量
*/
export const getCommentsByArticle = (articleId, offset) => {
  return request({
    url: '/v1_0/comments',
    params: { type: 'a', source: articleId, offset }
  })
}

/**
* 关注 和 取消关注
* @param {*} authorId - 作者ID
* @param {*} isLike - 是否点赞
* @returns Promise
*/
export const getLikesByArticle = (articleId, isLike) => {
  if (isLike) {
    return request({
      url: '/v1_0/article/likings',
      method: 'post',
      data: { target: articleId }
    })
  // 取消关注
  } else {
    return request({
      url: '/v1_0/article/likings/' + articleId,
      method: 'delete'
    })
  }
}

/**
* 关注 和 取消关注
* @param {*} authorId - 作者ID
* @param {*} isCollect - 是否收藏
* @returns Promise
*/
export const collectArticle = (articleId, isCollect) => {
  if (isCollect) {
    return request({
      url: '/v1_0/article/collections',
      method: 'post',
      data: { target: articleId }
    })
  // 取消收藏
  } else {
    return request({
      url: '/v1_0/article/collections/' + articleId,
      method: 'delete'
    })
  }
}

/**
* 获取文章评论
* @param {String} articleId - 文章ID
* @param {String} offset - 上一页数据最后一个ID，做为下一页请求的偏移量
*/
export const getReplyByArticle = (commentId, offset) => {
  return request({
    url: '/v1_0/comments',
    params: { type: 'c', source: commentId, offset }
  })
}

export const postCommentArticle = (target, content, articleId) => {
  return request({
    url: '/v1_0/comments',
    method: 'post',
    data: {
      target,
      content,
      art_id: articleId
    }
  })
}
